using UnityEngine;
using UnityEngine.UI;

namespace Naptime.Extensions
{
	public static class RectTransformExtension
	{
		public static void SetLeft(this RectTransform rt, float left)
		{
			rt.offsetMin = new Vector2(left, rt.offsetMin.y);
		}

		public static void SetRight(this RectTransform rt, float right)
		{
			rt.offsetMax = new Vector2(0f - right, rt.offsetMax.y);
		}

		public static void SetTop(this RectTransform rt, float top)
		{
			rt.offsetMax = new Vector2(rt.offsetMax.x, 0f - top);
		}

		public static void SetBottom(this RectTransform rt, float bottom)
		{
			rt.offsetMin = new Vector2(rt.offsetMin.x, bottom);
		}

		public static Vector2 CalculateFocusedScrollPosition(this ScrollRect scrollView, Vector2 focusPoint)
		{
			Vector2 size = scrollView.content.rect.size;
			Vector2 size2 = ((RectTransform)scrollView.content.parent).rect.size;
			Vector2 scale = scrollView.content.localScale;
			size.Scale(scale);
			focusPoint.Scale(scale);
			Vector2 normalizedPosition = scrollView.normalizedPosition;
			if (scrollView.horizontal && size.x > size2.x)
			{
				normalizedPosition.x = Mathf.Clamp01((focusPoint.x - size2.x * 0.5f) / (size.x - size2.x));
			}
			if (scrollView.vertical && size.y > size2.y)
			{
				normalizedPosition.y = Mathf.Clamp01((focusPoint.y - size2.y * 0.5f) / (size.y - size2.y));
			}
			return normalizedPosition;
		}

		public static Vector2 CalculateFocusedScrollPosition(this ScrollRect scrollView, RectTransform item)
		{
			Vector2 vector = scrollView.content.InverseTransformPoint(item.transform.TransformPoint(item.rect.center));
			Vector2 size = scrollView.content.rect.size;
			size.Scale(scrollView.content.pivot);
			return scrollView.CalculateFocusedScrollPosition(vector + size);
		}

		public static void FocusAtPoint(this ScrollRect scrollView, Vector2 focusPoint)
		{
			scrollView.normalizedPosition = scrollView.CalculateFocusedScrollPosition(focusPoint);
		}

		public static void FocusOnItem(this ScrollRect scrollView, RectTransform item)
		{
			scrollView.normalizedPosition = scrollView.CalculateFocusedScrollPosition(item);
		}

		public static void SetPivot2(this RectTransform rect, Vector2 pivot)
		{
			if (!(rect == null))
			{
				Vector3 vector = rect.pivot - pivot;
				vector.Scale(rect.rect.size);
				vector.Scale(rect.localScale);
				vector = rect.transform.localRotation * vector;
				rect.pivot = pivot;
				rect.localPosition -= vector;
			}
		}

		public static void SetPivot(this RectTransform target, Vector2 pivot)
		{
			if (!(target == null))
			{
				Vector2 vector = pivot - target.pivot;
				vector.Scale(target.rect.size);
				Vector3 position = target.position + target.TransformVector(vector);
				target.pivot = pivot;
				target.position = position;
			}
		}

		public static void SetAnchor(this RectTransform source, AnchorPresets allign, int offsetX = 0, int offsetY = 0)
		{
			source.anchoredPosition = new Vector3(offsetX, offsetY, 0f);
			switch (allign)
			{
			case AnchorPresets.TopLeft:
				source.anchorMin = new Vector2(0f, 1f);
				source.anchorMax = new Vector2(0f, 1f);
				break;
			case AnchorPresets.TopCenter:
				source.anchorMin = new Vector2(0.5f, 1f);
				source.anchorMax = new Vector2(0.5f, 1f);
				break;
			case AnchorPresets.TopRight:
				source.anchorMin = new Vector2(1f, 1f);
				source.anchorMax = new Vector2(1f, 1f);
				break;
			case AnchorPresets.MiddleLeft:
				source.anchorMin = new Vector2(0f, 0.5f);
				source.anchorMax = new Vector2(0f, 0.5f);
				break;
			case AnchorPresets.MiddleCenter:
				source.anchorMin = new Vector2(0.5f, 0.5f);
				source.anchorMax = new Vector2(0.5f, 0.5f);
				break;
			case AnchorPresets.MiddleRight:
				source.anchorMin = new Vector2(1f, 0.5f);
				source.anchorMax = new Vector2(1f, 0.5f);
				break;
			case AnchorPresets.BottomLeft:
				source.anchorMin = new Vector2(0f, 0f);
				source.anchorMax = new Vector2(0f, 0f);
				break;
			case AnchorPresets.BottonCenter:
				source.anchorMin = new Vector2(0.5f, 0f);
				source.anchorMax = new Vector2(0.5f, 0f);
				break;
			case AnchorPresets.BottomRight:
				source.anchorMin = new Vector2(1f, 0f);
				source.anchorMax = new Vector2(1f, 0f);
				break;
			case AnchorPresets.HorStretchTop:
				source.anchorMin = new Vector2(0f, 1f);
				source.anchorMax = new Vector2(1f, 1f);
				break;
			case AnchorPresets.HorStretchMiddle:
				source.anchorMin = new Vector2(0f, 0.5f);
				source.anchorMax = new Vector2(1f, 0.5f);
				break;
			case AnchorPresets.HorStretchBottom:
				source.anchorMin = new Vector2(0f, 0f);
				source.anchorMax = new Vector2(1f, 0f);
				break;
			case AnchorPresets.VertStretchLeft:
				source.anchorMin = new Vector2(0f, 0f);
				source.anchorMax = new Vector2(0f, 1f);
				break;
			case AnchorPresets.VertStretchCenter:
				source.anchorMin = new Vector2(0.5f, 0f);
				source.anchorMax = new Vector2(0.5f, 1f);
				break;
			case AnchorPresets.VertStretchRight:
				source.anchorMin = new Vector2(1f, 0f);
				source.anchorMax = new Vector2(1f, 1f);
				break;
			case AnchorPresets.StretchAll:
				source.anchorMin = new Vector2(0f, 0f);
				source.anchorMax = new Vector2(1f, 1f);
				break;
			case AnchorPresets.BottomStretch:
				break;
			}
		}
	}
}
